package 富婆梦.第一阶段.Java基础视频学习.线程的练习.作业2;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.线程的练习.作业二
 * @ClassName: PrintNunPlus
 * @Author: hemengjin
 * @Description:
 * @Date:2021/10/29 14:49
 * @Version: 1.0
 */
public class PrintNunPlus implements Runnable {
    //数字
    int num = 0;
    int max = 60;
    //用来标志当前现成本标号的count，使用完打印完一次之后就加一
    int threadCount = 1;
    List threadList = new ArrayList();

    private void addnum() {
        System.out.println("本次打印的线程是：" + Thread.currentThread().getName());
        for (int i = 0; i < 5; i++) {
            num = num + 1;
            System.out.print(num + ",");
        }
        System.out.println();
        threadCount = threadCount + 1;
        if (threadCount > 3) {
            threadCount = 1;
        }
    }

    private void judge(){

        synchronized (threadList){
            while (Integer.parseInt(Thread.currentThread().getName()) != threadCount){
                try {
                    threadList.wait();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if (num<max){
                addnum();
            }
            threadList.notifyAll();
        }
    }

    @Override
    public void run() {
        while (num < max) {
            judge();
        }
    }


    public static void main(String[] args) {
        PrintNunPlus printNumPlus = new PrintNunPlus();
        Thread t1 = new Thread(printNumPlus);
        Thread t2 = new Thread(printNumPlus);
        Thread t3 = new Thread(printNumPlus);
        t1.setName("1");
        t2.setName("2");
        t3.setName("3");
        t1.start();
        t2.start();
        t3.start();
        while (printNumPlus.num==printNumPlus.max) {
            break;
        }
    }
}
